

import 'package:extra_hittest_area/extra_hittest_area.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:furaha_mkopo/commonTool/fm_color_tool.dart';
import 'package:furaha_mkopo/commonTool/fm_jump_tool.dart';
import 'package:furaha_mkopo/commonTool/fm_language_tool.dart';
import 'package:furaha_mkopo/commonTool/fm_screen_tool.dart';
import 'package:furaha_mkopo/commonUI/fm_refresh_header.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../commonUI/fm_progress_hud.dart';
import 'fm_order_controller.dart';

class FMOrderSubPage extends StatefulWidget {

  final String listId;

  final bool isIdentify;

  const FMOrderSubPage({super.key,required this.listId,required this.isIdentify});

  @override
  State<FMOrderSubPage> createState() => _FMOrderSubPageState();
}

class _FMOrderSubPageState extends State<FMOrderSubPage> {

  final controller = Get.put(FMOrderController());

  RefreshController refreshController = RefreshController();

  @override
  void initState() {
    super.initState();
    controller.refreshController = refreshController;
    controller.refreshData(widget.listId);
    FMProgressHud.showLoading();
  }

  @override
  void dispose() {
    Get.delete<FMOrderController>();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<FMOrderController>(builder: (controller){
      if (controller.isEmpty) {
        return _emptyWidget();
      }
      return SmartRefresher(
         controller:refreshController,
         header: const FMRefreshHeader(),
         enablePullDown: true,
         child: _contentWidget(),
         onRefresh: (){
           controller.refreshData(widget.listId);
         },
      );
    });
  }
  
  Widget _emptyWidget() {
    String imageName = FMLanguageTool.getAssets("assets/order/order_${!widget.isIdentify ? "certification" : "uncertification"}_empty");
    return GestureDetector(
      onTap: (){
        Navigator.pop(context);
        FMJumpTool.jumpHomeWidget();
      },
      child: Center(
          child: Padding(
            padding: const EdgeInsets.only(bottom: 150),
            child: Image.asset(imageName,height: 133,fit: BoxFit.fill),
          )
      ),
    );
  }

  Widget _contentWidget() {
    return ListView.builder(
       padding: EdgeInsets.only(bottom: FMScreenTool.bottomPadding(context)),
       itemCount: controller.numberOfItems,
       itemBuilder: (context,index) {
         return _cellWidget(index);
       }
    );
  }

  Widget _cellWidget(int index) {
    return GestureDetector(
      onTap: (){
        jump(index);
      },
      child: Container(
        height: index == 0 ? 236 : 247,
        width: FMScreenTool.width(context) - 32,
        margin: const EdgeInsets.only(left: 16,right: 16),
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage(controller.orderImageAtIndex(index)),
            fit: BoxFit.fill
          )
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(height: index == 0 ? 10 : 24),
            _topWidget(index),
            SizedBox(height:index == 0 ? 10 : 8),
            Container(
              padding: const EdgeInsets.only(left: 12,right: 12),
              child: Image.asset('assets/order/order_cell_line.png')
            ),
            const SizedBox(height:10),
            _bottomWidget(index),
          ],
        ),
      ),
    );
  }

  Widget _topWidget(int index) {
    return Padding(
      padding: const EdgeInsets.only(left: 12,right: 20),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            children: [
              SizedBox(
                width: 30,
                height: 30,
                child: Image.network(
                  controller.logoAtIndex(index),
                  width: 30,
                  height: 30,
                  fit: BoxFit.cover,
                  errorBuilder: (context,ob,stack){
                    return Container();
                  },
                ),
              ),
              const SizedBox(width: 10),
              Text(
                controller.titleAtIndex(index),
                style: TextStyle(
                    fontWeight: FontWeight.w600,
                    color: FMColorTool.blackColor,
                    fontSize: 16
                ),
              ),
            ],
          ),
          Container(
            width: 117,
            height: 36,
            alignment: Alignment.center,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(6),
              color:FMColorTool.color("#178308"),
            ),
            child: Text(
              controller.statusTitleAtIndex(index),
              style: const TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.bold,
                color: Colors.white
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _bottomWidget(int index) {
    return Container(
      height: 151,
      margin: const EdgeInsets.only(left: 12,right: 12),
      decoration: const BoxDecoration(
          image: DecorationImage(
              image: AssetImage("assets/order/order_cell_bottom_bg.png"),
              fit: BoxFit.fill
          )
      ),
      child: Padding(
        padding: const EdgeInsets.only(left: 12,right: 12,top: 14),
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  controller.priceTitle(index),
                  style: TextStyle(
                    fontSize: 14,
                    fontWeight: FontWeight.w600,
                    color: FMColorTool.blackColor
                  ),
                ),
                Text(
                  controller.priceContent(index),
                  style: TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.w600,
                      color: FMColorTool.blackColor
                  ),
                )
              ],
            ),
            const SizedBox(height: 15),
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [ // 83 65
                Image.asset(controller.orderCircleImageName(index),width: 6,height: 65),
                const SizedBox(width: 10),
                Expanded(
                  child: SizedBox(
                    height: 92,
                    child: Column(
                      children: controller.subList(index).map((e){
                        int idx = controller.subList(index).indexOf(e);
                        return _bottomCellSubDescWidget(e.soul ?? "", e.patiently ?? "",index,needBold: idx == 3 ? true : false);
                      }).toList()
                    ),
                  )
                )
              ],
            )
          ],
        ),
      ),
    );
  }

  Widget _bottomCellSubDescWidget(String title,String desc,int index,{bool needBold = false}) {
    return Padding(
      padding: EdgeInsets.only(bottom: needBold ? 0 : 8),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          SizedBox(
            height: 17,
            child: Text(
              title,
              style: TextStyle(
                  fontSize: 14,
                  fontWeight: FontWeight.w500,
                  color: FMColorTool.color("#999999")
              ),
            ),
          ),
          GestureDetectorHitTestWithoutSizeLimit(
            extraHitTestArea: const EdgeInsets.all(10),
            onTap: (){
              if (needBold) {
                jumpProtocol(index);
              }
            },
            child: Text(
              desc,
              style: TextStyle(
                  fontSize: 14,
                  fontWeight: needBold ? FontWeight.w800 : FontWeight.w600,
                  color:needBold ? Colors.black : FMColorTool.blackColor
              ),
            ),
          ),
        ],
      ),
    );
  }

  void jump(int index) {
    String url = controller.jumpUrlAtIndex(index);
    if (url.isEmpty) return;

    FMJumpTool.jumpWidget(context: context,url: url);
  }

  void jumpProtocol(int index) {
    String url = controller.protocolUrl(index);
    if (url.isEmpty) return;

    FMJumpTool.jumpWebWidget(context: context,url: url);
  }
}
